package com.controller;

import com.common.dto.BaseIdReqDTO;
import com.common.dto.BaseIdsReqDTO;
import com.common.dto.PagingReqDTO;
import com.config.exception.BussServiceException;
import com.entity.Qu;
import com.entity.dto.QuDTO;
import com.entity.dto.ext.QuDetailDTO;
import com.entity.dto.request.QuQueryReqDTO;
import com.service.QuService;
import com.utils.PageUtils;
import com.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
* <p>
* 问题题目控制器
* </p>
*/
@Api(tags={"问题题目"})
@RestController
@RequestMapping("/exam/qu/qu")
public class QuController {

    @Autowired
    private QuService quService;

    /**
     * 添加或修改
     *
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "添加或修改")
    @RequestMapping(value = "/save", method = {RequestMethod.POST})
    public R save(@RequestBody QuDetailDTO reqDTO) throws BussServiceException {
        quService.save(reqDTO);
        return R.ok();
    }

    /**
     * 批量删除
     *
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "批量删除")
    @RequestMapping(value = "/delete", method = {RequestMethod.POST})
    public R edit(@RequestBody BaseIdsReqDTO reqDTO) {
        //根据ID删除
        quService.delete(reqDTO.getIds());
        return R.ok();
    }

    /**
     * 查找详情
     *
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "查找详情")
    @RequestMapping(value = "/detail", method = {RequestMethod.POST})
    public R detail(@RequestBody BaseIdReqDTO reqDTO) {
        QuDetailDTO dto = quService.detail(reqDTO.getId());
        return R.ok().put("data", dto);
    }

    /**
     * 分页查找
     *
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "分页查找")
    @RequestMapping(value = "/paging", method = {RequestMethod.POST})
    public R paging(@RequestBody PagingReqDTO<QuQueryReqDTO> reqDTO) {

        //分页查询并转换
        PageUtils page = quService.paging(reqDTO);

        return R.ok().put("data", page);
    }

    /**
     * 查找列表，每次最多返回200条数据
     *
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "查找列表")
    @RequestMapping(value = "/list", method = {RequestMethod.POST})
    public R list(@RequestBody QuDTO reqDTO) {
        //转换并返回
        List<Qu> list = quService.selectList(null);

        //转换数据
        List<QuDTO> dtoList = list.stream().map(v -> {
            QuDTO dto = new QuDTO();
            BeanUtils.copyProperties(v, dto);
            return dto;
        }).collect(Collectors.toList());

        return R.ok().put("data", dtoList);
    }
}
